package com.ttsx.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ttsx.pojo.dto.productDetail.ProductDetailQueryDTO;
import com.ttsx.pojo.entity.ProductDetail;
import com.ttsx.service.ProductDetailService;
import com.ttsx.core.common.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wb
 * @since 2025-06-05
 */

@RequestMapping("product-detail")
@RestController
public class ProductDetailController {

    @Autowired
    private ProductDetailService productDetailService;

    @PostMapping("page/{pIndex}/{pSize}")
    public Result<?> page(@PathVariable("pIndex") Integer pIndex, @PathVariable("pSize") Integer pSize, @RequestBody ProductDetailQueryDTO productDetailQueryDTO) {
        PageHelper.startPage(pIndex, pSize);
        List<ProductDetail> list = productDetailService.getByProductId(productDetailQueryDTO.getProductId());
        return Result.success(new PageInfo<>(list, 5));
    }


    @GetMapping("get/{id}")
    public Result<?> getById(@PathVariable("id") Long id) {
        return Result.success(productDetailService.getById(id));
    }

    @PostMapping("save")
    public Result<?> save(@RequestBody ProductDetail product) {
        return productDetailService.save(product) ? Result.success() : Result.failure();
    }

    @DeleteMapping("remove/{id}")
    public Result<?> delete(@PathVariable("id") Long id) {
        return productDetailService.removeById(id) ? Result.success() : Result.failure();
    }

}
